package com.servicesystem.servicesystem.service.ali.ali;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.servicesystem.servicesystem.entity.AliOrderLogistics;
import com.servicesystem.servicesystem.mapper.AliOrderLogisticsDao;
import com.servicesystem.servicesystem.service.ali.AliOrderService;
import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import com.servicesystem.servicesystem.untis.ali.AliData;
import com.servicesystem.servicesystem.untis.ali.AliSign;
import com.servicesystem.servicesystem.untis.ali.RequestUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AliOrderServiceImp implements AliOrderService {
    private final static HttpRequestAll httpRequestAll = new HttpRequestAll();

    @Autowired
    private AliOrderLogisticsDao aliOrderLogisticsDao;

    @Override
    public R getLogisticsInfo(String orderId) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("webSite", "1688");
            params.put("orderId", orderId);
            String signature = AliSign.getSignature("param2/1/com.alibaba.logistics/alibaba.trade.getLogisticsInfos.buyerView/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("webSite", "1688");
            objectObjectHashMap.put("orderId", orderId);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_ALI_LOGISTICS, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @Override
    public R getLogisticsInfoStream(String orderId) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("webSite", "1688");
            params.put("orderId", orderId);
            String signature = AliSign.getSignature("param2/1/com.alibaba.logistics/alibaba.trade.getLogisticsTraceInfo.buyerView/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("webSite", "1688");
            objectObjectHashMap.put("orderId", orderId);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_ALI_LOGISTICS_STREAM, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @Override
    public R checkLibraryTotal(String cargoParamList, String addressParam) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("addressParam", addressParam);
            params.put("cargoParamList", cargoParamList);
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.createOrder.preview4CybMedia/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("addressParam", addressParam);
            objectObjectHashMap.put("cargoParamList", cargoParamList);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_LIBRARY_TOTAL, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("网络错误");
    }
}
